﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginInterface;
using operacioninvalida;


namespace CalculadoraSimple
{
    public partial class Calculadora : Form
    {
        private Host Plugins;

        public Calculadora(){
            InitializeComponent();
            Plugins = new Host();
            Plugins.Actualizar();
            foreach(IPlugin p in Plugins.getPlugins()){ //agrego los plugins al combobox
                comboBox1.Items.Add(p.getNombre());
            }
        }

        protected void Actualizar(object sender, EventArgs e)
        {
            Plugins.Actualizar();
            comboBox1.Items.Clear();
            List<IPlugin> PlugsActivados = Plugins.getPlugins();
            foreach (IPlugin Plug in PlugsActivados)
            {
                comboBox1.Items.Add(Plug.getNombre());
            }
        }
        protected void obtenerResultado(object sender, EventArgs e)
        {
            String aux1 = textBox1.Text;
            String aux2 = textBox2.Text;
            double a = 0;
            double b = 0;
            Boolean huboerror = false;
            try
            { //convierto los operandos a double
                a = Convert.ToDouble(aux1);
                b = Convert.ToDouble(aux2);
            }
            catch (OverflowException)
            {
                MessageBox.Show("ERROR!:OverflowException");
                label1.Text = "ERROR";
                huboerror = true;
            }
            catch (FormatException)
            {
                MessageBox.Show("ERROR!:FormatException");
                label1.Text = "ERROR";
                huboerror = true;
            }
            if (!huboerror)
            {
                if (comboBox1.SelectedItem == null)
                {
                    MessageBox.Show("Seleccione una operacion");
                }
                else
                {
                    string tmp = (string)comboBox1.SelectedItem;
                    foreach (IPlugin p in Plugins.getPlugins())
                    {
                        if (p.getNombre() == tmp)
                        {
                            try
                            {
                                label1.Text = p.getResultado(a, b) + "";
                            }
                            catch (operacioninvalidaException exc)
                            {
                                MessageBox.Show(exc.Message);

                            }
                        }
                    }
                }
            }

        }

        
        protected void CalculadoraSimple_Load(object sender, EventArgs e)
        {

        }
    }
}
